#ifndef _MOTOR_H
#define _MOTOR_H
/**************************************************************************************************
 *                                            INCLUDES
 **************************************************************************************************/
#include <zephyr/kernel.h>
# include <stdio.h>
#include <stdlib.h>
#include <zephyr/drivers/uart.h>
#include <ctype.h>
#include <zephyr/sys/util.h>
#include <zephyr/device.h>
#include <zephyr/shell/shell_uart.h>
#include <string.h>
#include <zephyr/drivers/flash.h>
#include <zephyr/devicetree.h>

#define RS485_FRAME_LEN_CRC 13
#define RS485_FRAME_LEN     11


#define READ_PID                        0x30
#define WRITE_PID                       0x31
#define WRITE_PID_TOROM                 0x32
#define READ_ACC                        0x42
#define WRITE_ACC                       0x43
#define READ_ENCODER_POS                0x60
#define READ_ENCODER_POS_ORIGINAL       0x61
#define READ_ENCODER_INIT_POS           0x62
#define WRITE_ENCODER_INIT_POS          0x63
#define WRITE_ENCODER_INIT_POS_TOROM    0x64
#define READ_ABS_ANGLE                  0x92
#define READ_STATUS_ONE                 0x9A
#define READ_STATUS_TWO                 0x9C
#define MOTOR_SHUTDOWM                  0x80
#define MOTOR_STOP                      0x81
#define MOTOR_RESET_SYSTEM              0x76
#define TORQUE_CLOSEDLOOP_CTR           0xA1
#define SPEED_CLOSEDLOOP_CTR            0xA2
#define POS_TRACK_CLOSEDLOOP_CTR        0xA3
#define ABS_POS_CLOSEDLOOP_CTR          0xA4
#define POS_TRACK_CTR_SPEEDLIMIT        0xA5
#define POS_INC_CLOSEDLOOP_CTR          0xA8

#define RS485_FRAME_HEAD                0x3E

typedef struct __attribute__ ((__packed__)){
	uint8_t frame_header;
	uint8_t id;
	uint8_t length;
	uint8_t cmd;
	uint8_t null;
	uint8_t data[6];
	uint8_t crc16L;
    uint8_t crc16H;
}Motor_Ctr;

typedef struct __attribute__ ((__packed__)){ 
    uint8_t motor_tem; //单位 摄氏度
    int32_t motor_acc; //单位 dps/s
    int32_t encoder_pos; //单位 脉冲
    int32_t encoder_pos_original; //单位 脉冲
    int32_t encoder_pos_init;  //单位 脉冲
    float motor_spd; //单位dps
    float motor_vol;   //单位 V
    float motor_curr; //单位 A
    float motor_degree; //单位 度
    float abs_angle;  //单位 度
    float CurrKP,CurrKI,SpdKP,SpdKI,PosKP,PosKI;
}Motor_Para;

int Motor_Ctr_API(const struct device *uart_dev, Motor_Ctr rs485_frame);
struct k_sem *find_semName_fromCmd(uint8_t cmd);
Motor_Ctr Read_PID(void);
Motor_Ctr Write_PID(uint8_t Ckp,uint8_t Cki,uint8_t Skp,uint8_t Ski,uint8_t Pkp,uint8_t Pki);
Motor_Ctr Write_PID_toROM(uint8_t Ckp,uint8_t Cki,uint8_t Skp,uint8_t Ski,uint8_t Pkp,uint8_t Pki);
Motor_Ctr Read_Acc(void);
Motor_Ctr Write_Acc(uint32_t acc);
Motor_Ctr Read_Encoder_pos(void);
Motor_Ctr Read_Encoder_pos_original(void);
Motor_Ctr Read_Encoder_init_pos(void);
Motor_Ctr Write_Encoder_init_pos(int32_t pos);
Motor_Ctr Write_Encoder_init_pos_toROM(int32_t pos);
Motor_Ctr Read_abs_angle(void);
Motor_Ctr Read_Status_one(void);
Motor_Ctr Read_Status_two(void);
Motor_Ctr Motor_Shutdown(void);
Motor_Ctr Motor_Stop(void);
Motor_Ctr Motor_Reset_system(void);
Motor_Ctr Torque_ClosedLoop_ctr(uint32_t toque);
Motor_Ctr Speed_ClosedLoop_ctr(uint32_t speed);
Motor_Ctr Pos_Track_closedLoop_ctr(int32_t pos);
Motor_Ctr Abs_Pos_closedLoop_ctr(int32_t abs,uint16_t maxSpeed);
Motor_Ctr Pos_Track_ctr_speedLimit(int32_t pos,uint16_t maxSpeed);
Motor_Ctr Pos_Inc_closedLoop_ctr(int32_t pos,uint16_t maxSpeed);
void uart2_rx_irq_cb(const struct device *uart_dev,void *user_data);
void rs485_rx_dispense(void *p1, void *p2, void *p3);
int motor_init(const struct device *uart_dev);
uint16_t CRC16_ModBus(uint8_t *puchMsg, uint16_t usDataLen);

extern Motor_Para Motor_Para_g;



#endif    //_MOTOR_H
